const StudentService = require('../../service/student')
const Response = require('../../utils/response')

async function newStudent(ctx) {
    ctx.request.body.status = 0
    const response = new Response()
    const result = await StudentService.newStudent({
        ctx
    })
    if (result.result) {
        response.SUCCESS = 200
        response.DATA = result
    } else {
        response.FAIL = 500
        response.DATA = '错误'
    }
    ctx.body = response.getData()
}

async function getAllStudent(ctx) {
    const result = await StudentService.getAllStudent({ctx})
    const response = new Response()
    response.SUCCESS = 200
    response.DATA = {
        count: result.length,
        Students: result
    }
    ctx.body = response.getData()
}

async function getStudentById(ctx) {
    const { id } = ctx.params
    const { student } = await StudentService.getStudentById({ ctx, id })
    const response = new Response()

    if(student) {
      response.SUCCESS = 200
      response.DATA = student
    }else{
      response.FAIL = 500
      response.DATA = '记录不存在'
    }
    ctx.body = response.getData()
}


async function delStudent(ctx) {
    const {
        id
    } = ctx.request.body

    const result = await StudentService.delStudent({
        id
    })

    const response = new Response()

    if (result) {
        response.SUCCESS = 200
        response.DATA = '删除成功'
    } else {
        response.FAIL = 500
        response.DATA = '删除错误'
    }
    ctx.body = response.getData()
}

async function setStudent(ctx) {
    const {
        id
    } = ctx.request.body
    const result = await StudentService.setStudent({
        ctx,
        id
    })
    const response = new Response()
    if (result) {
        response.SUCCESS = 200
        response.DATA = '修改成功'
    } else {
        response.FAIL = 500
        response.DATA = '错误'
    }
    ctx.body = response.getData()
}

module.exports = {
    newStudent,
    getAllStudent,
    delStudent,
    setStudent,
    getStudentById
}